package com.heima.controller;

import com.heima.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

@RestController
public class DeptController {
    @Value("${dept.deptPath}")
    String deptPath;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/findAll")
    public Collection<Dept> findAll(){
        //return restTemplate.getForEntity("http://localhost:8082/dept/findAll", Collection.class).getBody();
        return restTemplate.getForEntity(deptPath+"findAll", Collection.class).getBody();
    }
    @GetMapping("/findById/{id}")
    public Dept findById(@PathVariable("id") Integer id){
        //return restTemplate.getForEntity("http://localhost:8082/dept/findById/"+id, Dept.class).getBody();
        return restTemplate.getForEntity(deptPath+"findById/"+id, Dept.class).getBody();
    }
    @PostMapping("/save")
    public void save(@RequestBody Dept dept){
        restTemplate.postForEntity(deptPath+"save",dept,null).getBody();
    }
    @PutMapping("/update")
    public void update(@RequestBody Dept dept){
        restTemplate.put(deptPath+"update",dept);
    }
    @DeleteMapping("/deleteById/{id}")
    public void delete(@PathVariable("id") Integer id){
        restTemplate.delete(deptPath+"delete/"+id,id);
    }
}
